package com.haven.webrobotserver.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.haven.webrobotserver.dao.mybatis.mapper.UserMapper;
import com.haven.webrobotserver.domain.User;
import com.haven.webrobotserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
@RequestMapping("/user")
public class UserController {

    @Autowired(required=false)
    private UserService userService;

    @PostMapping("/getUsers")
    public JSONObject getUsers(@RequestParam int pageSize, @RequestParam int currentPage,
                               @RequestBody Map<String,String> params){

        JSONObject result = new JSONObject();

        PageInfo<User> pageUsersByCondition = userService.getPageUsersByCondition(params, pageSize, currentPage);



        String s = JSONObject.toJSONString(pageUsersByCondition.getList());
        JSONArray objects = JSONObject.parseArray(s);


        result.put("status",200);
        result.put("data",objects);
        result.put("total",pageUsersByCondition.getTotal());

        return result;
    }

}
